package com.king.zxing.app;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.king.zxing.app.domain.User;
import com.king.zxing.app.fragments.IntegrationFragment;
import com.king.zxing.app.fragments.MainFragment;
import com.king.zxing.app.fragments.MallFragment;
import com.king.zxing.app.fragments.PersonalCenterFragment;

import java.util.ArrayList;
import java.util.List;

import kotlin.Triple;

public class TabActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab);

        Bundle bundle = getIntent().getExtras();
        assert bundle != null;

        User user = bundle.getParcelable("user");

        ViewPager2 viewPager = findViewById(R.id.view_pager);
        viewPager.setUserInputEnabled(false);
        TabLayout tabLayout = findViewById(R.id.tab_layout);

        FragmentStateAdapter fragmentAdapter = getFragmentStateAdapter();

        // 设置 ViewPager2 的适配器
        viewPager.setAdapter(fragmentAdapter);

        List<Triple<TabLayout.Tab, Integer, Integer>> picture_list = new ArrayList<>();
        final int[] last_position = {0};
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if (tab.getPosition() == 2) {
                    Intent intent3 = new Intent(TabActivity.this, QRCodeScanActivity.class);
                    startActivity(intent3);
                    tabLayout.selectTab(tabLayout.getTabAt(last_position[0]));
                    return;
                }
                last_position[0] = tab.getPosition();
                for (Triple<TabLayout.Tab, Integer, Integer> item : picture_list) {
                    if (tab.getPosition() == item.component1().getPosition()) {
                        tab.setIcon(item.component3());
                    }
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                for (Triple<TabLayout.Tab, Integer, Integer> item : picture_list) {
                    if (tab.getPosition() == item.component1().getPosition()) {
                        tab.setIcon(item.component2());
                    }
                }
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });

        // 使用 TabLayoutMediator 实现 TabLayout 和 ViewPager2 的双向绑定
        new TabLayoutMediator(tabLayout, viewPager, (tab, position) -> {
            switch (position) {
                case 0:
                    tab.setText("首页");
                    tab.setIcon(R.drawable.main_page_1);
                    picture_list.add(new Triple<>(tab, R.drawable.main_page_0, R.drawable.main_page_1));
                    break;
                case 1:
                    tab.setText("积分");
                    tab.setIcon(R.drawable.score_0);
                    picture_list.add(new Triple<>(tab, R.drawable.score_0, R.drawable.score_1));
                    break;
                case 2:
                    tab.setText("扫码");
                    tab.setIcon(R.drawable.qr_scan);
                    picture_list.add(new Triple<>(tab, R.drawable.qr_scan, R.drawable.qr_scan));
                    break;
                case 3:
                    tab.setText("商城");
                    tab.setIcon(R.drawable.goods_0);
                    picture_list.add(new Triple<>(tab, R.drawable.goods_0, R.drawable.goods_1));
                    break;
                case 4:
                    tab.setText("个人中心");
                    tab.setIcon(R.drawable.personal_center_0);
                    picture_list.add(new Triple<>(tab, R.drawable.personal_center_0, R.drawable.personal_center_1));
                    break;
            }
        }).attach();
    }

    @NonNull
    private FragmentStateAdapter getFragmentStateAdapter() {

        return new FragmentStateAdapter(TabActivity.this) {
            @NonNull
            @Override
            public Fragment createFragment(int position) {
                switch (position) {
                    case 1:
                        return new IntegrationFragment();
                    case 3:
                        return new MallFragment();
                    case 4:
                        return new PersonalCenterFragment();
                    default:
                        return new MainFragment();
                }
            }

            @Override
            public int getItemCount() {
                return 5;
            }
        };
    }


}
